/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.patterns;


import java.util.ArrayList;
import java.util.List;

import org.digitalsoul.switchplugin.SwitchFile;


public class CompoundPattern implements IPattern {

    private List<IPattern> patterns;
    private int matches;
    
    public List<IPattern> getPatterns() {
        return patterns;   
    }
    
    public CompoundPattern() {
        patterns = new ArrayList<IPattern>();
    }

    public void add(IPattern pattern) {
        patterns.add(pattern);
    }
    
    public SwitchFile applyToFile(IPattern previousPattern, SwitchFile file) {
        for (IPattern pattern : patterns) {
            file = pattern.applyToFile(previousPattern, file);
        }
        return file;
    }
    
    public int getMatches() {
        return matches;
    }

    public boolean matches(SwitchFile file) {
        matches = 0;
        for (IPattern pattern : patterns) {
            if (pattern.matches(file)) {
                matches++;
            }
        }
        return matches == patterns.size();
    }

    /**
     * @see org.digitalsoul.switchplugin.patterns.IPattern#undoFile(SwitchFile)
     */
    public SwitchFile undoFile(SwitchFile file) {
        for (IPattern pattern : patterns) {
            file = pattern.undoFile(file);
        }
        return file;
    }

    public PatternType getType() {
        return PatternType.COUMPUND_PATTERN;
    }
    
    @Override
    public String toString() {
        StringBuffer buffy = new StringBuffer();
        for (IPattern pat: patterns) {
            buffy.append(pat.toString());
            buffy.append(" :: ");
        }
        return buffy.toString();
    }
}
